﻿using IMeiWeiWebService.Models;
using IMeiWeiWebService.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IMeiWeiWebService.Extensions;

namespace IMeiWeiWebService.Managers
{
    public class DataServiceManager
    {
        private AreaProvider _areaProvider = new AreaProvider();
        private SenceClassificationProvider _senceProvider = new SenceClassificationProvider();
        private BusinessClassProvider _businessClassProvider = new BusinessClassProvider();
        private IMeiWei.BLL.businessimgs bll_imgs = new IMeiWei.BLL.businessimgs();
        private IMeiWei.BLL.business bll_Business = new IMeiWei.BLL.business();
        private IMeiWei.BLL.article bll_article = new IMeiWei.BLL.article();
        private BusinessSelectItemProvider _businessSelectItemProvider = new BusinessSelectItemProvider();
        private AdProvider _adProvider = new AdProvider();
        private const string IMEIWEISTRING = "D385CF8F-B6E6-437C-81D6-33E0B6EF33DD";
        private const string LIFESTRING = "EF74E123-455B-461F-83B3-E2C0D46AFA09";

        public IMeiWeiConfiguration LoadIMeiWeiConfiguration()
        {
            return ConstructIMeiWeiConfiguration();
        }

        public HappyLiveConfiguration LoadHappyLiveConfiguration()
        {
            return ConstructLoadHappyLiveConfiguration();
        }

        public bool UpdateArticleReadNo(string articleId, int readNo)
        {
            var model = bll_article.GetModel(articleId);

            model.ReadNo = readNo;
            bll_article.Update(model);

            return true;
        }

        #region Private method

        private HappyLiveConfiguration ConstructLoadHappyLiveConfiguration()
        {
            HappyLiveConfiguration result = new HappyLiveConfiguration();

            result.Cities = ConstructCities();
            result.Scenes = ConstructScenes();
            result.ClassOnes = ConstructClassOnes();
            result.Merchants = ConstructMerchant(LIFESTRING);
            result.CoverImageInfos = ConstructCoverImageInfos();

            return result;
        }

        private IMeiWeiConfiguration ConstructIMeiWeiConfiguration()
        {
            IMeiWeiConfiguration result = new IMeiWeiConfiguration();

            result.Cities = ConstructCities();
            result.Scenes = ConstructScenes();
            result.Cuisines = ConstructCuisines();
            result.Merchants = ConstructMerchant(IMEIWEISTRING);
            result.CoverImageInfos = ConstructCoverImageInfos();

            return result;
        }

        private List<City> ConstructCities()
        {
            List<City> result = new List<City>();
            var areaOnes = _areaProvider.LoadAreaOneLeval();
            var areaTwos = _areaProvider.LoadAreaTwoLeval();

            if (null != areaOnes && areaOnes.Count > 0 && null != areaTwos && areaTwos.Count > 0)
            {
                foreach (var oneItem in areaOnes)
                {
                    var areaTwoItems = areaTwos.FindAll(o => o.AreaOne_Id == oneItem.Id);

                    if (null != areaTwoItems && areaTwoItems.Count > 0)
                    {
                        List<Area> twoItemResult = new List<Area>();
                        foreach (var twoItem in areaTwoItems)
                        {
                            twoItemResult.Add(twoItem.ToArea());
                        }
                        result.Add(oneItem.ToCity(twoItemResult));
                    }
                }
            }

            return result;
        }

        private List<Scene> ConstructScenes()
        {
            var sences = _senceProvider.LoadSenceClassificationes();

            List<Scene> result = new List<Scene>();

            if (null != sences && sences.Count > 0)
            {
                foreach (var item in sences)
                {
                    result.Add(item.ToScene());
                }
            }

            return result;
        }

        private List<Item> ConstructClassOnes()
        {
            List<Item> result = new List<Item>();

            var classOnes = _businessClassProvider.LoadClassOneItems(IMEIWEISTRING);
            var classTwoes = _businessClassProvider.LoadClassTwoItems();

            foreach (var oneItem in classOnes)
            {
                var classTwoItems = classTwoes.FindAll(o => o.BusinessTypeClassOne_Id == oneItem.Id);
                if (null != classTwoItems && classTwoItems.Count > 0)
                {
                    var subCuisines = new List<SubItem>();
                    foreach (var twoItem in classTwoItems)
                    {
                        subCuisines.Add(twoItem.ToSubItem());
                    }
                    result.Add(oneItem.ToItem(subCuisines));
                }
            }

            return result;
        }

        private List<Cuisine> ConstructCuisines()
        {
            List<Cuisine> result = new List<Cuisine>();

            var classOnes = _businessClassProvider.LoadClassOneItems(IMEIWEISTRING);
            var classTwoes = _businessClassProvider.LoadClassTwoItems();

            foreach (var oneItem in classOnes)
            {
                var classTwoItems = classTwoes.FindAll(o => o.BusinessTypeClassOne_Id == oneItem.Id);
                if (null != classTwoItems && classTwoItems.Count > 0)
                {
                    var subCuisines = new List<SubCuisines>();
                    foreach (var twoItem in classTwoItems)
                    {
                        subCuisines.Add(twoItem.ToSubCuisines());
                    }
                    result.Add(oneItem.ToCuisine(subCuisines));
                }
            }

            return result;
        }

        private List<Merchant> ConstructMerchant(string appType)
        {
            List<Merchant> result = new List<Merchant>();
            var businessRecords = bll_Business.GetModelList(" BusinessTypeId='" + appType + "' ");
            var businessImgs = bll_imgs.GetModelList(" OrderNo='" + 1 + "' ");
            var selectItems = _businessSelectItemProvider.LoadBusinessSelectItem(appType);
            var selectDetails = _businessSelectItemProvider.LoadBusinessSelectDetail();

            string classOne = string.Empty;
            string classTwo = string.Empty;

            if (appType == IMEIWEISTRING)
            {
                classOne = "主营菜系";
                classTwo = "选择菜系";
            }
            else
            {
                classOne = "主营项目";
                classTwo = "选择类别";
            }

            if (null != businessRecords && businessRecords.Count > 0)
            {
                foreach (var businessRecord in businessRecords)
                {
                    string classTwoName = string.Empty;
                    var selectItemCuisine = selectItems.Where(o => o.SelectItemName == classTwo).FirstOrDefault();
                    if (null != selectItemCuisine)
                    {
                        var AppCuisineItem = selectDetails.Where(o => o.Item_Id == selectItemCuisine.Id && o.Business_Id == businessRecord.BusinessId).FirstOrDefault();
                        classTwoName = null == AppCuisineItem ? string.Empty : AppCuisineItem.SelectItem_Id;
                    }
                    var businessImg = businessImgs.FirstOrDefault(o => o.Business_Id == businessRecord.BusinessId);
                    string imgUrl = businessImg == null ? string.Empty : businessImg.picURL;
                    result.Add(businessRecord.ToMerchant(imgUrl, classTwoName));
                }

            }

            return result;
        }
        #endregion
        private List<ImageInfo> ConstructCoverImageInfos()
        {
            List<ImageInfo> result = new List<ImageInfo>();
            var imgs = _adProvider.LoadAdImages("IMEIWEI");
            if (null != imgs && imgs.Count > 0)
            {
                foreach (var item in imgs)
                {
                    result.Add(new ImageInfo()
                    {
                        UrlName = item.UrlName,
                    });
                }
            }
            return result;
        }
    }
}